﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using MONTRASEC_test.Classes;

namespace MONTRASEC_test.Controls
{
  public partial class wucDVC2Victims : System.Web.UI.UserControl
  {
    //copy this part
    public DetailsView dv
    {
      //change the DetailsView field name
      get { return dvC2Victims; }
      set { dvC2Victims = value; }
    }
    public BulletedList BPleaseFillOtherTemplates
    {
      get { return bPleaseFillOtherTemplates; }
      set { bPleaseFillOtherTemplates = value; }
    }
    public Label LbTotalRecords
    {
      get { return lbTotalRecords; }
      set { lbTotalRecords = value; }
    }

    public Label LDVMode
    {
      get { return lbDVMode; }
      set { lbDVMode = value; }
    }

    //until here

    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void dvC2Victims_ItemInserting(object sender, DetailsViewInsertEventArgs e)
    {
      e.Values["C_2_VICTIM_DIGEST"] = UtilHelper.getHash(e.Values["C_2_VICTIM_PERSONALDATA_IDENTITY_NAME"], e.Values["C_2_VICTIM_PERSONALDATA_NATIONALITY"], e.Values["C_2_VICTIM_PERSONALDATA_DATEOFBIRTHOFTHEVICTIM"]);
      e.Values["C_2_VICTIM_DIGEST_NFP"] = UtilHelper.getHash(UtilHelper.getHash(e.Values["C_2_VICTIM_DIGEST"]));
      e.Values["C_2_VICTIM_PERSONALDATA_DATEOFBIRTHOFTHEVICTIM"] = UtilHelper.getDateTime(e.Values["C_2_VICTIM_PERSONALDATA_DATEOFBIRTHOFTHEVICTIM"]);
    }

    protected void dvC2Victims_ItemUpdating(object sender, DetailsViewUpdateEventArgs e)
    {
      e.NewValues["C_2_VICTIM_DIGEST"] = UtilHelper.getHash(e.NewValues["C_2_VICTIM_PERSONALDATA_IDENTITY_NAME"], e.NewValues["C_2_VICTIM_PERSONALDATA_NATIONALITY"], e.NewValues["C_2_VICTIM_PERSONALDATA_DATEOFBIRTHOFTHEVICTIM"]);
      e.NewValues["C_2_VICTIM_DIGEST_NFP"] = UtilHelper.getHash(UtilHelper.getHash(e.NewValues["C_2_VICTIM_DIGEST"]));
      e.NewValues["C_2_VICTIM_PERSONALDATA_DATEOFBIRTHOFTHEVICTIM"] = UtilHelper.getDateTime(e.NewValues["C_2_VICTIM_PERSONALDATA_DATEOFBIRTHOFTHEVICTIM"]);

    }
    protected void detailsView_ModeChanged(object sender, EventArgs e)
    {
      onDetailsView_ModeChanged(e);
    }
    // the delegate; a delegate is a reference type used to encapsulate a method with a specific signature and return type.* You can encapsulate any matching method in that delegate.
    public delegate void detailsView_ModeChangedHandler(object sender, EventArgs e);

    // an instance of the delegate
    public event detailsView_ModeChangedHandler DetailsView_ModeChanged;

    protected virtual void onDetailsView_ModeChanged(EventArgs e)
    {
      if (DetailsView_ModeChanged != null)
      {
        DetailsView_ModeChanged(this, e);
      }
    }

    protected void detailsView_ItemDeleted(object sender, DetailsViewDeletedEventArgs e)
    {
      onDetailsView_ItemDeleted(e);
    }
    // the delegate; a delegate is a reference type used to encapsulate a method with a specific signature and return type.* You can encapsulate any matching method in that delegate.
    public delegate void detailsView_ItemDeletedHandler(object sender, EventArgs e);

    // an instance of the delegate
    public event detailsView_ItemDeletedHandler DetailsView_ItemDeleted;

    protected virtual void onDetailsView_ItemDeleted(EventArgs e)
    {
      if (DetailsView_ItemDeleted != null)
      {
        DetailsView_ItemDeleted(this, e);
      }
    }

    protected void detailsView_ItemInserted(object sender, DetailsViewInsertedEventArgs e)
    {
      onDetailsView_ItemInserted(e);
    }
    // the delegate; a delegate is a reference type used to encapsulate a method with a specific signature and return type.* You can encapsulate any matching method in that delegate.
    public delegate void detailsView_ItemInsertedHandler(object sender, EventArgs e);

    // an instance of the delegate
    public event detailsView_ItemInsertedHandler DetailsView_ItemInserted;

    protected virtual void onDetailsView_ItemInserted(EventArgs e)
    {
      if (DetailsView_ItemInserted != null)
      {
        DetailsView_ItemInserted(this, e);
      }
    }

    protected void detailsView_ItemUpdated(object sender, DetailsViewUpdatedEventArgs e)
    {

      onDetailsView_ItemUpdated(e);
    }
    // the delegate; a delegate is a reference type used to encapsulate a method with a specific signature and return type.* You can encapsulate any matching method in that delegate.
    public delegate void detailsView_ItemUpdatedHandler(object sender, EventArgs e);

    // an instance of the delegate
    public event detailsView_ItemUpdatedHandler DetailsView_ItemUpdated;

    protected virtual void onDetailsView_ItemUpdated(EventArgs e)
    {
      if (DetailsView_ItemUpdated != null)
      {
        DetailsView_ItemUpdated(this, e);
      }
    }

    protected void replicateARow(object sender, DetailsViewCommandEventArgs e)
    {
      String myMessage = ""; //message listing the id's that have been updated
      DetailsView dv = (DetailsView)sender;
      int index = int.Parse(e.CommandArgument.ToString());

      String id = (dv.DataKey["ID"]).ToString();

      MONTRASEC_test.Classes.A_NEWENTRY_DataContext dataContext = new MONTRASEC_test.Classes.A_NEWENTRY_DataContext();

      var newEntryToCopy =
          from b in dataContext.GetTable<A_NEWENTRY_>()
          where b.ID == long.Parse(id)
          select b;



      foreach (A_NEWENTRY_ c in newEntryToCopy)
      {
        var recordsToWhichToReplicate =
          from listOfRecordsToReplicate in dataContext.GetTable<A_NEWENTRY_>()
          where listOfRecordsToReplicate.A_NEWENTRY_NEWENTRY_POLICE_CUSTOMSFILEIDENTIF.Equals(c.A_NEWENTRY_NEWENTRY_POLICE_CUSTOMSFILEIDENTIF)
          && listOfRecordsToReplicate.A_NEWENTRY_NEWENTRY_PUBLICPROSECUTORSFILEIDEN.Equals(c.A_NEWENTRY_NEWENTRY_PUBLICPROSECUTORSFILEIDEN)
          && listOfRecordsToReplicate.A_NEWENTRY_NEWENTRY_COURTSFILEIDENTIFICATIONN.Equals(c.A_NEWENTRY_NEWENTRY_COURTSFILEIDENTIFICATIONN)
          && listOfRecordsToReplicate.ID != c.ID
          select listOfRecordsToReplicate;

        foreach (A_NEWENTRY_ recordToWhichToReplicate in recordsToWhichToReplicate)
        {

          recordToWhichToReplicate.C_2_Victim_DIGEST = c.C_2_Victim_DIGEST;
          recordToWhichToReplicate.C_2_Victim_DIGEST_NFP = c.C_2_Victim_DIGEST_NFP;
          recordToWhichToReplicate.C_2_VICTIM_PERSONALDATA_IDENTITY_NAME = c.C_2_VICTIM_PERSONALDATA_IDENTITY_NAME;
          recordToWhichToReplicate.C_2_VICTIM_PERSONALDATA_ALIAS = c.C_2_VICTIM_PERSONALDATA_ALIAS;
          recordToWhichToReplicate.C_2_VICTIM_PERSONALDATA_NICKNAME = c.C_2_VICTIM_PERSONALDATA_NICKNAME;
          recordToWhichToReplicate.C_2_VICTIM_PERSONALDATA_GENDEROFTHEVICTIM = c.C_2_VICTIM_PERSONALDATA_GENDEROFTHEVICTIM;
          recordToWhichToReplicate.C_2_VICTIM_PERSONALDATA_DATEOFBIRTHOFTHEVICTIM = c.C_2_VICTIM_PERSONALDATA_DATEOFBIRTHOFTHEVICTIM;
          recordToWhichToReplicate.C_2_VICTIM_PERSONALDATA_AGEOFSEXUALCONSENTINYOURC = c.C_2_VICTIM_PERSONALDATA_AGEOFSEXUALCONSENTINYOURC;
          recordToWhichToReplicate.C_2_VICTIM_PERSONALDATA_VICTIMISUNDERTHEAGEOFCONS = c.C_2_VICTIM_PERSONALDATA_VICTIMISUNDERTHEAGEOFCONS;
          recordToWhichToReplicate.C_2_VICTIM_PERSONALDATA_COUNTRYOFBIRTH = c.C_2_VICTIM_PERSONALDATA_COUNTRYOFBIRTH;
          recordToWhichToReplicate.C_2_VICTIM_PERSONALDATA_PLACEOFBIRTH = c.C_2_VICTIM_PERSONALDATA_PLACEOFBIRTH;
          recordToWhichToReplicate.C_2_VICTIM_PERSONALDATA_NATIONALITY = c.C_2_VICTIM_PERSONALDATA_NATIONALITY;
          recordToWhichToReplicate.C_2_VICTIM_PERSONALDATA_FINGERPRINTS = c.C_2_VICTIM_PERSONALDATA_FINGERPRINTS;
          recordToWhichToReplicate.C_2_VICTIM_PERSONALDATA_CRIMINALRECORD = c.C_2_VICTIM_PERSONALDATA_CRIMINALRECORD;
          recordToWhichToReplicate.C_2_VICTIM_PERSONALDATA_MARITALSTATUS = c.C_2_VICTIM_PERSONALDATA_MARITALSTATUS;
          recordToWhichToReplicate.C_2_VICTIM_PERSONALDATA_STRUCTUREOFTHEFAMILYOFORI = c.C_2_VICTIM_PERSONALDATA_STRUCTUREOFTHEFAMILYOFORI;
          recordToWhichToReplicate.C_2_VICTIM_PERSONALDATA_EDUCATIONINCLUDESPRIMARYS = c.C_2_VICTIM_PERSONALDATA_EDUCATIONINCLUDESPRIMARYS;
          recordToWhichToReplicate.C_2_VICTIM_PERSONALDATA_LIVINGSITUATIONOFTHEVICTI = c.C_2_VICTIM_PERSONALDATA_LIVINGSITUATIONOFTHEVICTI;
          recordToWhichToReplicate.C_2_VICTIM_PERSONALDATA_COUNTRYOFRESIDENCE = c.C_2_VICTIM_PERSONALDATA_COUNTRYOFRESIDENCE;
          recordToWhichToReplicate.C_2_VICTIM_PERSONALDATA_PLACEOFRESIDENCE = c.C_2_VICTIM_PERSONALDATA_PLACEOFRESIDENCE;
          recordToWhichToReplicate.C_2_VICTIM_PERSONALDATA_OCCUPATIONOFTHEVICTIM = c.C_2_VICTIM_PERSONALDATA_OCCUPATIONOFTHEVICTIM;
          recordToWhichToReplicate.C_2_VICTIM_LIAISONBETWEENTHEVICTIMSA_LIAISON = c.C_2_VICTIM_LIAISONBETWEENTHEVICTIMSA_LIAISON;
          recordToWhichToReplicate.C_2_VICTIM_MONEYREQUESTED_PAID_VICTIMIS_SOLD_FROMONEGROU = c.C_2_VICTIM_MONEYREQUESTED_PAID_VICTIMIS_SOLD_FROMONEGROU;
          recordToWhichToReplicate.C_2_VICTIM_MONEYREQUESTED_PAID_PRICEPAIDFORTHEVICTIM = c.C_2_VICTIM_MONEYREQUESTED_PAID_PRICEPAIDFORTHEVICTIM;
          recordToWhichToReplicate.C_2_VICTIM_RE_VICTIMISATION_RE_VICTIMISATION = c.C_2_VICTIM_RE_VICTIMISATION_RE_VICTIMISATION;
          recordToWhichToReplicate.C_2_VICTIM_RE_VICTIMISATION_OFTHESAMECRIME = c.C_2_VICTIM_RE_VICTIMISATION_OFTHESAMECRIME;
          recordToWhichToReplicate.C_2_VICTIM_RE_VICTIMISATION_AREAOFEXPLOITATION = c.C_2_VICTIM_RE_VICTIMISATION_AREAOFEXPLOITATION;
          recordToWhichToReplicate.C_2_VICTIM_ATV_FIRSTASSISTANCEREFERR_POLICE = c.C_2_VICTIM_ATV_FIRSTASSISTANCEREFERR_POLICE;
          recordToWhichToReplicate.C_2_VICTIM_ATV_FIRSTASSISTANCEREFERR_JUDICIALAUTHORITY = c.C_2_VICTIM_ATV_FIRSTASSISTANCEREFERR_JUDICIALAUTHORITY;
          recordToWhichToReplicate.C_2_VICTIM_ATV_FIRSTASSISTANCEREFERR_NGO = c.C_2_VICTIM_ATV_FIRSTASSISTANCEREFERR_NGO;
          recordToWhichToReplicate.C_2_VICTIM_ATV_FIRSTASSISTANCEREFERR_GO = c.C_2_VICTIM_ATV_FIRSTASSISTANCEREFERR_GO;
          recordToWhichToReplicate.C_2_VICTIM_ATV_FIRSTASSISTANCEREFERR_HOSPITALORMEDICALFACILITI = c.C_2_VICTIM_ATV_FIRSTASSISTANCEREFERR_HOSPITALORMEDICALFACILITI;
          recordToWhichToReplicate.C_2_VICTIM_ATV_FIRSTASSISTANCEREFERR_INDIVIDUAL = c.C_2_VICTIM_ATV_FIRSTASSISTANCEREFERR_INDIVIDUAL;
          recordToWhichToReplicate.C_2_VICTIM_ATV_FIRSTASSISTANCEREFERR_HOTLINE = c.C_2_VICTIM_ATV_FIRSTASSISTANCEREFERR_HOTLINE;
          recordToWhichToReplicate.C_2_VICTIM_ATV_FIRSTASSISTANCEREFERR_OUTREACHSERVICES = c.C_2_VICTIM_ATV_FIRSTASSISTANCEREFERR_OUTREACHSERVICES;
          recordToWhichToReplicate.C_2_VICTIM_ATV_FIRSTASSISTANCEREFERR_OTHER = c.C_2_VICTIM_ATV_FIRSTASSISTANCEREFERR_OTHER;
          recordToWhichToReplicate.C_2_VICTIM_ATV_INSTITUTIONPROVIDINGA_INSTITUTION = c.C_2_VICTIM_ATV_INSTITUTIONPROVIDINGA_INSTITUTION;
          recordToWhichToReplicate.C_2_VICTIM_ATV_KINDOFASSISTANCEPROVI_GRANT_SCHEME = c.C_2_VICTIM_ATV_KINDOFASSISTANCEPROVI_GRANT_SCHEME;
          recordToWhichToReplicate.C_2_VICTIM_ATV_KINDOFASSISTANCEPROVI_EDUCATION = c.C_2_VICTIM_ATV_KINDOFASSISTANCEPROVI_EDUCATION;
          recordToWhichToReplicate.C_2_VICTIM_ATV_KINDOFASSISTANCEPROVI_LEGALCOUNSELLING = c.C_2_VICTIM_ATV_KINDOFASSISTANCEPROVI_LEGALCOUNSELLING;
          recordToWhichToReplicate.C_2_VICTIM_ATV_KINDOFASSISTANCEPROVI_MEDICALREFERRALORASSISTAN = c.C_2_VICTIM_ATV_KINDOFASSISTANCEPROVI_MEDICALREFERRALORASSISTAN;
          recordToWhichToReplicate.C_2_VICTIM_ATV_KINDOFASSISTANCEPROVI_FAMILYSUPPORT = c.C_2_VICTIM_ATV_KINDOFASSISTANCEPROVI_FAMILYSUPPORT;
          recordToWhichToReplicate.C_2_VICTIM_ATV_KINDOFASSISTANCEPROVI_PSYCHOLOGICALASSISTANCE = c.C_2_VICTIM_ATV_KINDOFASSISTANCEPROVI_PSYCHOLOGICALASSISTANCE;
          recordToWhichToReplicate.C_2_VICTIM_ATV_KINDOFASSISTANCEPROVI_HOUSING = c.C_2_VICTIM_ATV_KINDOFASSISTANCEPROVI_HOUSING;
          recordToWhichToReplicate.C_2_VICTIM_ATV_KINDOFASSISTANCEPROVI_SHELTER = c.C_2_VICTIM_ATV_KINDOFASSISTANCEPROVI_SHELTER;
          recordToWhichToReplicate.C_2_VICTIM_ATV_KINDOFASSISTANCEPROVI_OTHER = c.C_2_VICTIM_ATV_KINDOFASSISTANCEPROVI_OTHER;

          dataContext.SubmitChanges();
          myMessage += recordToWhichToReplicate.ID + " - ";
        } //foreach




      } //foreach

      if (myMessage.Length > 0)
      {
        myMessage = "Entries that have been updated - " + myMessage;
      }
      else
      {
        myMessage = "No entries have updated";
      }

      System.Web.HttpContext.Current.Response.Write("<SCRIPT LANGUAGE='JavaScript'>");
      System.Web.HttpContext.Current.Response.Write("alert('" + myMessage + "')");
      System.Web.HttpContext.Current.Response.Write("</SCRIPT>");
    }

  }
}